package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址簿 前端控制器
 * </p>
 *
 * @author fcy
 * @since 2025-09-12
 */
@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "C端地址簿接口")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result<String> addAddress(@RequestBody AddressBook addressBook){
        return addressBookService.addAddress(addressBook);
    }

    /**
     * 查看地址
     * 查询当前用户全部地址
     *
     */
    @ApiOperation("查看地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list =addressBookService.getList();
        return Result.success(list);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }
    /**
     * 设置默认地址
     *
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){
        Integer id =Integer.valueOf(addressBook.getId().toString());
        return addressBookService.setDefault(id);
    }

    /**
     * 修改地址
     *
     */
    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){
        return addressBookService.change(addressBook);
    }
    /**
     * 删除地址
     *
     */
    @ApiOperation("删除地址")
    @DeleteMapping
    public Result<String> delete(Integer id){
        return addressBookService.deleteById(id);
    }

}
